import formVation from 'wechat-validate'

Page({
  behaviors:[formVation],
  rules:{
    name:[
      {required:true,message:"业主姓名不能为空"},
      {pattern:/^(?:[\u4e00-\u9fa5·]{2,16})$/,message:"业主姓名只能为中文" }
    ],
    mobile:[
      {required:true,message:"手机号码不能为空"},
      {pattern:/^(?:(?:\+|00)86)?1\d{10}$/,message:"手机号码不符合格式"}
    ],
    houseName:[{required:true,message:"房屋信息不能为空"}],
    visitDate:[{required:true,message:"访问日期不能为空"}]
  },
  data: {
    dateLayerVisible: false,
    houseLayerVisible: false,
    houseList: [],
    // 房屋信息
    houseId:"",
    houseName:"",
    name:"",
    gender:"0",
    mobile:"",
    minDate:new Date(wx.utils.getDatePicker(1)).getTime(),
    maxDate:new Date(wx.utils.getDatePicker(4)).getTime(),
    visitDate:""
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  // 访客详情
  async goPassport() {
    // console.log(111);
    const res = this.validate()
    if(!res) return 
     const {code,data} = await wx.myRequest.post("/visitor",{
      houseId:this.data.houseId,
      name:this.data.name,
      gender:this.data.gender,
      mobile:this.data.mobile,
      visitDate:this.data.visitDate
    })
    if(code !== 10000) return wx.utils.toast("请求失败！")
    // console.log(data);
    wx.reLaunch({
      url: '/visitor_pkg/pages/passport/index?id='+data.id,
    })
  },
  onLoad(){
    // 获取房屋信息
    this.getHouseListInfo()
  },
  // 获取房屋信息
  async getHouseListInfo(){
    const {code,data} = await wx.myRequest.get("/house")
    if(code !== 10000) return wx.utils.toast("获取房屋信息失败")
    this.setData({
      houseList:data
    })
  },
  // 选择房屋
  onHouseSelect(ev){
   this.setData({
    houseId:ev.detail.id,
    houseName:ev.detail.name
   })
  },
  // 选择时间
  dateConfirmfn(ev){
    const date = new Date(ev.detail)
    let years = date.getFullYear()
    let month = (date.getMonth()+1)
    if (month > 12) {
      years+=1
      month-=12
    }
    const day = date.getDate()
    
    this.setData({
      dateLayerVisible:false,
      visitDate:`${years}-${month}-${day}`
    })

  }

})
